<template>
  <Container class="fund-check-list">
    <HeadFormItem
      slot="header"
      :list="list"
      @onSearchBtnClick="onSearchBtnClick"
    />
    <Tabs v-model="tab">
      <export-btn @onExport="onExport" class="item" slot="extra"></export-btn>
      <TabPane label="门店基金审核" name="tab1">
         <homeTable
          :list="list"
          @getData="getData"
          @onPageSizeChange="onPageSizeChange"
          @onPageChange="onPageChange"
        />
      </TabPane>
    </Tabs>
  </Container>
</template>
<script>
import FundCheckListData from './list/FundCheckListData'
import HeadFormItem from './list/HeadFormItem'
import homeTable from '@/components/homeTable'
import { clearEmpty, formatDate } from '@/util'
export default {
  name: 'welfare/fundcheck/list',
  components: {
    HeadFormItem,
    homeTable
  },
  data() {
    return {
      tab: "tab1",
      list: FundCheckListData
    };
  },
  mounted() {
    this.getData();
    this.$bus.$on('detailRow', ({row}) => {this.detailRow(row)})
    this.$bus.$on('lookRow', ({row}) => {this.lookRow(row)})
    this.$bus.$on('checkRow', ({row}) => {this.checkRow(row)})
  },
  destroyed() {
    this.$bus.$off();
  },
  methods: {
    onSearchBtnClick(form) {
      this.list.currentPage = 1;
      this.list.form = form
      this.getData();
    },
    checkRow(row) {
      let checkQuery = {...this.list.form}
      if (this.list.form.time) {
        let month = formatDate(this.list.form.time).split('-')[1]
        checkQuery.year = formatDate(this.list.form.time).split('-')[0]
        checkQuery.month = month.charAt(0) == 0 ? month.charAt(1) : month
      }
      delete checkQuery.addressArr
      delete checkQuery.departmentObj
      delete checkQuery.time
      checkQuery = clearEmpty(checkQuery)
      window.localStorage.setItem('checkQuery', JSON.stringify(checkQuery))
      this.$router.push({
        name: 'welfare/fundcheck/info',
        query: {
          id: row.id,
          from: 'check'
        }
      })
    },
    lookRow(row) {
      this.$router.push({
        name: 'welfare/fundcheck/info',
        query: {
          id: row.id,
          from: 'look'
        }
      })
    },
    detailRow(row) {
      this.$router.push({
        name: 'welfare/fundcheck/detail',
        query: {
          id: row.id
        }
      })
    },
    getData() {
      this.list.status = "loading"
      let v = this.formatParam();
      if (!v) return
      let param = {
        ...this.formatParam(),
        current: this.list.currentPage,
        size: this.list.pageSize
      }
      param = clearEmpty(param);
      this.$ajaxGet("/assets/fund_month/page", param).then(data => {
        this.list.data = [];
        this.list.totalItem = Number(data.total)
        this.list.data = (data.records || []).map(item => {
          return {
            id: item.id,
            storeName: item.storeName,
            distributorName: item.distributorName,
            storeTypeName: item.storeTypeName,
            storeNo: item.storeNo,
            storeContactName:item.storeContactName,
            storePhone: item.storePhone,
            createDate: item.createDate,
            address: (item.provinceName ? item.provinceName : '') + (item.cityName ? item.cityName : '') + (item.areaName ? item.areaName : ''),
            score: item.score,
            scoreOperateName: item.scoreOperateName,
            statusMeaning: item.statusMeaning,
            auditOperateName: item.auditOperateName,
            increaseQuantity: item.increaseQuantity,
            reduceQuantity: item.reduceQuantity,
            abnormalQuantity: item.abnormalQuantity,
            waitAuditFund: item.waitAuditFund,
            auditFundAmount: item.auditFundAmount,
            isCanAudit: item.isCanAudit,
            status: item.status,
            blockName: item.blockName,
            departmentName: item.departmentName
          }
        })
        this.list.status = "pass"
      }).catch(() => {
        this.list.status = "error"
      })
    },
    formatParam() {
      let param = {
        status: this.list.form.status === '4' ? null : this.list.form.status,
        provinceId: this.list.form.provinceId ? this.list.form.provinceId : null,
        distributorId: this.list.form.distributorId ? this.list.form.distributorId : null
      }
      if (this.list.form.time) {
        let month = formatDate(this.list.form.time).split('-')[1]
        param.year = formatDate(this.list.form.time).split('-')[0]
        param.month = month.charAt(0) == 0 ? month.charAt(1) : month
      }
      param = clearEmpty(param)
      return param
    },
    onExport() {
      if (this.list.data.length === 0) {
        this.$Notice.warning({
          title: `暂无数据，无法导出`
        })
        return
      }
      let v = this.formatParam();
      if (!v) return
      let param = {
        ...this.formatParam()
      }
      param = clearEmpty(param);
      this.$jsonPost('/assets/fund_month/export', param).then(data => {
        this.list.status = 'pass'
        this.$Modal.success({
          title: '导出成功',
          content: '请到首页-导出列表处下载',
          onOk: () => {
            this.$router.push('/home/export/list')
          }
        })
      }).catch(err => {
        this.list.status = 'pass'
        this.$Notice.error({
          title: '导出失败'
        })
      })
    },
    onPageChange(number) {
      this.list.currentPage = number
      this.getData()
    },
    onPageSizeChange(pageSize) {
      this.list.currentPage = 1;
      this.list.pageSize = pageSize;
      this.getData()
    },
    onCheckAll() {
      if (this.list.data.length === 0) {
        this.$Notice.warning({
          title: `列表无数据，不能一键审核`
        })
        return
      }
      if (!this.list.form.time) {
        this.$Notice.warning({
          title: `请先选择月份`
        })
        return
      }
      let month = formatDate(this.list.form.time).split('-')[1]
      let param = {
        year: formatDate(this.list.form.time).split('-')[0],
        month: month.charAt(0) == 0 ? month.charAt(1) : month
      }
      this.$Modal.confirm({
        title: '确认一键审核搜索的结果？',
        content:  '一键审核后，当前筛选的月份任务已完成的将自动把已计算好的返利审核给门店。任务未完成的将自动把返利审核成0或负数，并审核完成。且以上操作不可逆。',
        onOk: () => {
          this.$jsonPost('/assets/fund_month/audit/all', param).then(() => {
            this.$Notice.success({
              title: `一键审核成功`
            })
            this.getData()
          })
        }
      })
    }
  },
  watch: {
  }
};
</script>
